package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.data.HeroParam;
import com.myk.game.heroscuffle.events.EventRunBack;
import com.myk.game.heroscuffle.events.HeroEvent_AfterAction;
import com.myk.game.heroscuffle.game.HateRecorder;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Skill_ChuiJi extends Skill {

    public static final int DAMAGE = 120;
    public static final double CHAOS_RATE = 1 / 4d;

    public Skill_ChuiJi() {
        super("锤击",
                "对目标英雄造成大量伤害，同时让其精确度(指服从玩家的指定和反击敌人的概率)大大降低，直到目标下次行动结束后。",
                3, Integer.MAX_VALUE);
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            Hero hero = hostHero.autoSelectAttacked().getTarget();
            skillEffect(hero);
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选" + SKILL_NAME + "施放的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    private void skillEffect(Hero target) {
        //造成伤害
        game.ui.printMsg("抡起大锤猛地砸向" + target.getObviousName() + ",");
        target.sufferDamage(null, this, null, DAMAGE);
        game.ui.sleepShortly();
        if (target.state.isAlive) {
            //造成混乱
            game.ui.printMsg(target.getObviousName() + "头晕目眩,命中率大大降低。");
            game.ui.sleepShortly();
            target.state.exactRate = CHAOS_RATE;
            target.event.onAfterAction.addRunBack(eventData -> { //目标行动结束后混乱会恢复
                target.state.exactRate = HeroParam.EXACT_RATE;
                eventData.removeRunBackFromListener();
            });
            //记录仇恨
            //target.setEnemy(hostHero);
            game.recorder.record(game.round,hostHero,target, HateRecorder.HATE_TYPE_EFFECT);
        }
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {
        remnantCoolingTime = 0;
    }
}
